import sun.font.StrikeCache;

import java.util.Arrays;
import java.util.Stack;

//模拟栈的实现
public class Mystack {
    public int[] elem;
    public int usedSize;
    public Mystack(){
        this.elem=new int[10];
    }
    public void push(int val){
        if (isFull()){
            //扩容
            elem= Arrays.copyOf(elem,2*elem.length);//数组名 新的长度
        }
        elem[usedSize++]=val;
    }
    //判断是否满
    public boolean isFull(){
        return usedSize== elem.length;
    }
    //出栈
    public int pop(){
        if (isEmpty()){
            throw new EmptyException("栈是空的");
        }
        int val=elem[--usedSize];
        return val;
    }
    //判断是否为空
    public boolean isEmpty(){
        return usedSize==0;//usedsize=0返回true,否则返回false
    }
    //获取栈顶元素
    public int  peek(){
        if (isEmpty()){
            throw new EmptyException("栈是空的");
        }
        int val=elem[usedSize-1];
        return val;
    }
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch=s.charAt(i);
            if (ch=='('||ch=='['||ch=='{'){//左括号的情况
                stack.push(ch);
            }else {//遍历到字符数组的右括号
                if (stack.isEmpty()){//判断栈是否为空
                    return false;//栈为空，右括号找不到左括号匹配
                }
                char ch2=stack.peek();//接收左括号
                if (ch2=='('&&ch==')'||ch2=='['&&ch==']'||ch2=='{'&&ch=='}'){
                    //代表匹配
                    stack.pop();//匹配就把左括号出栈
                }else {
                    return false;//不匹配
                }
            }
        }
        if (!stack.isEmpty()){//isEmpty!=0,返回false，在非一次表示等于0为真
            return false;
        }
        return true;
    }
    //栈的压入，弹出序列
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack=new Stack<>();
        int j=0;
        for (int i = 0; i < pushV.length; i++) {
            //入栈
            stack.push(pushV[i]);
            while(j<popV.length&&!stack.isEmpty()&&stack.peek().equals(popV[j])){//Integer范围-128~127,注意Integer越界
                stack.pop();//匹配出栈
                j++;
            }
        }
        return stack.empty();
        
    }

}
